 //
 //  WBNetwork+Extension.swift
 //  Weibo
 //
 //  Created by Albert Zhu on 16/10/7.
 //  Copyright © 2016年 Albert Zhu. All rights reserved.
 //
 
 import Foundation
 import UIKit
 
 // MARK: - 封装新浪微博的网络请求方法
 extension WBNetworkManager {
    /// 加载微博数据
    func statusList(since_id: Int64 = 0, max_id: Int64 = 0, completion: @escaping (_ list: [[String: AnyObject]]?, _ isSuccess: Bool)->()) {
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let params = ["since_id":"\(since_id)", "max_id":"\(max_id > 0 ? max_id - 1 : 0)"]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            if isSuccess {
                
                let list = json as AnyObject?
                
                let result = list?["statuses"] as? [[String:AnyObject]]
                
                completion(result, isSuccess)
            }
        }
    }
    
    func unreadCount(completion: @escaping (Int)->()) -> (){
        
        guard let uid = userAccount.uid else {
            return
        }
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        let params = ["uid":uid]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            debugPrint(json)
            
            let dict = json as? [String:AnyObject]
            let count = dict?["status"] as? Int
            
            completion(count ?? 0)
        }
    }
    
    /// OAuth相关方法
    func loadAccessToken(code: String, completion:@escaping (_ isSuccess: Bool)->()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let params = ["client_id":WBAppKey, "client_secret":WBAppSecurity, "grant_type":"authorization_code", "code":code, "redirect_uri":"https://m.baidu.com"]
        
        request(method: .POST, URLString: urlString, parameters: params) { (json, isSuccess) in
            if isSuccess {
                self.userAccount.yy_modelSet(with: (json as? [String: AnyObject]) ?? [:])
                
                debugPrint(self.userAccount)
                
                self.loadUserInfo{ json in
                    self.userAccount.yy_modelSet(with: json)
                
                    self.userAccount.save()

                    completion(isSuccess)
                }
            }
        }
    }
 }
 
 // MARK: - 用户信息
 extension WBNetworkManager {
    func loadUserInfo(completion: @escaping (_ dict: [String:AnyObject])->()) {
        
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = ["uid":uid]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            if isSuccess {
                completion(json as? [String:AnyObject] ?? [:])
            }
        }
    }
 }
 
// MARK: - 发送微博
 extension WBNetworkManager {
    /// 发布微博
    ///
    /// - parameter text:       文本
    /// - parameter completion: 回调
    func postStatus(_ text: String, image: UIImage?, completion: @escaping ([String:AnyObject]?, Bool)->()) -> () {
        let urlString: String
        var name: String?
        var data: Data?
        
        if image == nil {
            urlString = "https://api.weibo.com/2/statuses/update.json"
        }else{
            urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
            name = "pic"
            data = UIImagePNGRepresentation(image!)
        }
        
        let params = ["status":text]
        tokenRequest(method: .POST, URLString: urlString, parameters: params, name: name, data: data) { (json, isSuccess) in
            completion(json as? [String:AnyObject], isSuccess)
        }
    }
 }
